import java.util.ArrayList;
import java.util.List;

//https://leetcode.cn/problems/combinations/
class Solution {
    List<List<Integer>> ret;
    List<Integer> path;

    public List<List<Integer>> combine(int n, int k) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        dfs(n, k, 1);
        return ret;
    }

    public void dfs(int n, int k, int pos) {
        //函数出口
        if (path.size() == k) {
            ret.add(new ArrayList<>(path));
            return;
        }
        //自动剪枝 -> 从当前元素的下一个位置开始枚举
        for (int i = pos; i <= n; i++) {
                path.add(i);
                dfs(n, k, i + 1);
                //回溯(恢复现场)
                path.remove(path.size() - 1);
        }
    }
}